import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'logic.dart';

class GeneratePage extends StatefulWidget {
  @override
  _GeneratePageState createState() => _GeneratePageState();
}

class _GeneratePageState extends State<GeneratePage> {
  bool _obscureText = true; // 密码隐藏状态
  //final TextEditingController _pwdController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    final logic = Get.find<GenKeyLogic>();
    return Scaffold( // 添加 Scaffold
      body:Padding(
      padding: const EdgeInsets.all(16.0),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.stretch,
        children: [
          // 密码输入框
          TextFormField(
            controller: logic.pwdController,
            obscureText: _obscureText,
            decoration: InputDecoration(
              isCollapsed: true, // 关键属性[1](@ref)
              contentPadding: const EdgeInsets.symmetric(
                  vertical: 6, // 垂直间距控制高度
                  horizontal: 6),
              labelText: '登录密码',
              hintText: '请输入8-20个字符的密码',
              border: OutlineInputBorder(),
              suffixIcon: IconButton(
                icon: Icon(
                  _obscureText ? Icons.visibility_off : Icons.visibility,
                  color: Colors.grey,
                ),
                onPressed: () {
                  setState(() {
                    _obscureText = !_obscureText;
                  });
                },
              ),
            ),
            validator: (value) {
              if (value == null || value.isEmpty) {
                return '密码不能为空';
              }
              if (value.length < 8 || value.length > 20) {
                return '密码长度需为8-20个字符';
              }
              return null;
            },
          ),
          const SizedBox(height: 20),

          Row(
            children: [
              Expanded(
                child: ElevatedButton.icon(
                  icon: const Icon(Icons.enhanced_encryption), // 修正图标名称
                  label: const Text('生成AES密钥'),
                  style: ElevatedButton.styleFrom(
                    padding: const EdgeInsets.symmetric(vertical: 16),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(8),
                    ),
                  ),
                  onPressed: logic.generateAESKey,
                ),
              ),
              const SizedBox(width: 10),
              Expanded(
                child: ElevatedButton.icon(
                  icon: const Icon(Icons.vpn_key),
                  label: const Text('生成ECC密钥对'),
                  style: ElevatedButton.styleFrom(
                    padding: const EdgeInsets.symmetric(vertical: 16),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(8),
                    ),
                  ),
                  onPressed: logic.generateECCKeyPair,
                ),
              ),
            ],
          ),
          const SizedBox(height: 20),
          Expanded(
            child: Container(
              padding: const EdgeInsets.all(16),
              decoration: BoxDecoration(
                border: Border.all(color: Colors.blue.shade200),
                borderRadius: BorderRadius.circular(12),
              ),
              child: Obx(() => SingleChildScrollView(
                child: SelectableText(
                  logic.result.value,
                  style: const TextStyle(
                    fontFamily: 'RobotoMono',
                    fontSize: 14,
                    height: 1.6,
                  ),
                ),
              )),
            ),
          ),
        ],
      ),
    )
    );
  }
}
